package org.lanqiao.taru.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import   org.lanqiao.taru.interceptor.TokenInterceptor;
/**
 * 这是一个配置类
 */
@Configuration
public class WebConfig  implements WebMvcConfigurer {

    @Value("${my.uploadPath}")
    private  String  uploadFilePath;



    @Autowired
    private  TokenInterceptor  tokenInterceptor;

    /**
     * 专门用来配置 静态资源的
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //静态资源映射
        registry.addResourceHandler("/static/**").addResourceLocations("file:"+uploadFilePath);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }


    /**
     * 跨域配置
     *  allowedOrigins  允许的域
     *  allowedHeaders  允许的头
     *  allowedMethods 允许的方法
     *  allowCredentials  是否允许携带凭证（cookie）
     *  maxAge  预检缓存时间
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
       // response.setHeader("Access-Control-Allow-Origin", *)
       // response.setHeader(("Access-Control-Allow-Credentials", true);)
       // response.setHeader("Access-Control-Request-Method", "PUT,POST,GET,DELETE,OPTIONS");
        registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*").allowCredentials(false).maxAge(3600);
    }


    /**
     * 用来添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       // registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns("/static/**","/userinfo/login","/webjars/**","/doc.html","/swagger-resources");
    }
}








